import unittest
from time import sleep
from poium import Page,Element
from selenium import webdriver

class LoginPage(Page):
    login = Element(css='#g-j-signin-btn')
    account_button = Element(css='span[data-type="tuzhan"]')
    username = Element(css='[placeholder="请输入你的登录账户"]')
    password = Element(css='[placeholder="请输入密码"]')
    login_button = Element(css='button.g-btn.login-btn.do-btn')
    error_tip = Element(css='div.login-form span.error-tips')

class Testlogin(unittest.TestCase):

    def test_login_case_fail(self):
        page = self.tuzhan_login(account='****', password='ssss')
        self.assertEqual(page.error_tip.text, "登录密码错误")

    def test_login_case_success(self):
        page = self.tuzhan_login(account='*****', password='****')
        self.assertIn("工作台", page.get_title)

    def tuzhan_login(self, account, password):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(2)

        page = LoginPage(self.driver)
        page.get("https://www.rabbitpre.com/")
        page.login.click()

        login_iframe = self.driver.find_element_by_css_selector('#sso')
        self.driver.switch_to.frame(login_iframe)

        page.account_button.click()
        sleep(2)
        page.username = account
        page.password = password
        page.login_button.click()
        sleep(2)
        return page

if __name__ =='__main__':
    unittest.main()